package avicit.bdp.dcs.json.service.writer;

import avicit.bdp.common.utils.database.DBUtils;
import avicit.bdp.core.util.json.JSONUtils;
import avicit.bdp.dcs.datasource.dto.JobDatasource;
import avicit.bdp.dcs.datasource.service.DataSourceUtils;
import avicit.bdp.dcs.desensitization.dto.DcsDesensitizationPolicyDTO;
import avicit.bdp.dcs.desensitization.dto.DcsDesensitizationRuleDTO;
import avicit.bdp.dcs.desensitization.service.DesensitizationPolicyService;
import avicit.bdp.dcs.desensitization.service.DesensitizationRuleService;
import avicit.bdp.dcs.job.dto.JobDTO;
import avicit.bdp.dcs.json.service.util.BuildJsonUtil;
import avicit.bdp.dcs.plugin.BaseDataxParam;
import avicit.bdp.dcs.plugin.odps.param.OdpsDataxParam;
import avicit.bdp.dcs.task.dto.ColumnMappingDto;
import avicit.bdp.dcs.task.dto.TaskDTO;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Component
public class OdpsWriterBuilder {

  @Autowired private DataSourceUtils dataSourceUtils;
  @Autowired private DesensitizationRuleService desensitizationRuleService;
  @Autowired private DesensitizationPolicyService desensitizationPolicyService;
  @Autowired private BuildJsonUtil buildJsonUtil;

  /** 构建ODPS采集Writer */
  public BaseDataxParam buildOdpsWriterByStructure(TaskDTO taskDTO, JobDTO jobDTO) throws Exception {
    // 根据列映射关系转换列名
    Map<String, String> columnMap = new HashMap<>(16);
    List<ColumnMappingDto> columnMappingDtoList =
        JSONUtils.toList(taskDTO.getMappingColumn(), ColumnMappingDto.class);
    if (CollectionUtils.isNotEmpty(columnMappingDtoList)) {
      for (ColumnMappingDto columnMappingDto : columnMappingDtoList) {
        columnMap.put(columnMappingDto.getSrcColumnName(), columnMappingDto.getDstColumnName());
      }
    }

    // 脱敏策略
    List<ColumnMappingDto> policyList = new ArrayList<>();
    if (CollectionUtils.isNotEmpty(columnMappingDtoList)) {
      for (ColumnMappingDto columnMappingDto : columnMappingDtoList) {
        if (StringUtils.isNotEmpty(columnMappingDto.getDesensitizationRuleId())
            && StringUtils.isNotEmpty(columnMappingDto.getDesensitizationPolicyId())) {
          DcsDesensitizationRuleDTO desensitizationRuleDTO =
              desensitizationRuleService.selectByPrimaryKey(
                  columnMappingDto.getDesensitizationRuleId());
          DcsDesensitizationPolicyDTO desensitizationPolicyDTO =
              desensitizationPolicyService.selectByPrimaryKey(
                  columnMappingDto.getDesensitizationPolicyId());
          if (desensitizationRuleDTO != null && desensitizationPolicyDTO != null) {
            columnMappingDto.setDesensitizationRuleId(desensitizationRuleDTO.getName());
            columnMappingDto.setDesensitizationPolicyId(desensitizationPolicyDTO.getName());
            policyList.add(columnMappingDto);
          }
        }
      }
    }

    // 构建OdpsWriter
    JobDatasource writerDatasource = dataSourceUtils.convertDatasource(jobDTO.getDstDatasourceId());
    OdpsDataxParam dataxParam = new OdpsDataxParam();
    dataxParam.setAccessId(writerDatasource.getJdbcUsername());
    dataxParam.setAccessKey(writerDatasource.getJdbcPassword());
    dataxParam.setProject(writerDatasource.getDatabaseName());
    dataxParam.setOdpsServer(writerDatasource.getBaseDataSource().getIp());
    dataxParam.setTable(DBUtils.convertTableName(writerDatasource.getDbType(), taskDTO.getDstTableName()));
    dataxParam.setWriterPartition("");
    dataxParam.setColumnList(buildJsonUtil.getWriterColumns(taskDTO, writerDatasource.getDbType()));
    dataxParam.setTruncate(true);
    dataxParam.setTunnelServer("");
    dataxParam.setAccountType("aliyun");
    return dataxParam;
  }

}
